package com.leetcode;

/**
 * 905. 按奇偶排序数组
 * 双指针原地交换 指针两头都动
 * 减少交换次数
 *
 * @author fy
 * @date 2022/4/28 9:53
 */
public class Solution905_4 {

    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = n - 1;
        while (left < right) {
            while (left < right && nums[left] % 2 == 0) {
                left++;
            }
            while (left < right && nums[right] % 2 == 1) {
                right--;
            }
            if (left < right) {
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
                left++;
                right--;
            }
        }
        return nums;
    }

}
